package drds.binlog.database_driver.packets.client;

import drds.binlog.common.Authors;
import drds.binlog.database_driver.packets.CommandPacket;
import drds.binlog.database_driver.utils.Bytes;
import drds.common.Author;
import lombok.Getter;
import lombok.Setter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * COM_REGISTER_SLAVE
 */
@Author(name = Authors.LI_YANG)
public class RegisterSlaveCommandPacket extends CommandPacket
{

    @Setter
    @Getter
    public String reportHost;
    @Setter
    @Getter
    public int reportPort;
    @Setter
    @Getter
    public String reportUser;
    @Setter
    @Getter
    public String reportPasswd;
    @Setter
    @Getter
    public long serverId;

    public RegisterSlaveCommandPacket()
    {
        setCommand((byte) 0x15);
    }


    public void fromBytes(byte[] bytes)
    {
        // bypass
    }

    public byte[] toBytes() throws IOException
    {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byteArrayOutputStream.write(getCommand());
        Bytes.writeUnsigned4IntLittleEndian(byteArrayOutputStream, serverId);
        byteArrayOutputStream.write((byte) reportHost.getBytes().length);
        Bytes.writeFixedLengthBytes(byteArrayOutputStream, reportHost.getBytes(), reportHost.getBytes().length);
        byteArrayOutputStream.write((byte) reportUser.getBytes().length);
        Bytes.writeFixedLengthBytes(byteArrayOutputStream, reportUser.getBytes(), reportUser.getBytes().length);
        byteArrayOutputStream.write((byte) reportPasswd.getBytes().length);
        Bytes.writeFixedLengthBytes(byteArrayOutputStream, reportPasswd.getBytes(), reportPasswd.getBytes().length);
        Bytes.writeUnsigned2IntLittleEndian(byteArrayOutputStream, reportPort);
        Bytes.writeUnsigned4IntLittleEndian(byteArrayOutputStream, 0);// Fake
        // rpl_recovery_rank
        Bytes.writeUnsigned4IntLittleEndian(byteArrayOutputStream, 0);// master id
        return byteArrayOutputStream.toByteArray();
    }

}
